# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libgme
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.6.4
pkgrel=2
pkgdesc="Video game music file emulation/playback library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/libgme/game-music-emu"
msys2_references=(
  "cpe: cpe:/a:game-music-emu_project:game-music-emu"
)
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('!emptydirs')
source=("https://github.com/libgme/game-music-emu/releases/download/${pkgver}/${_realname}-${pkgver}-src.tar.gz"
         libgme-3afa3ea124c5360ad561d7acb7ba97ae7497841d.patch)
sha256sums=('6f94eac735d86bca998a7ce1170d007995191ef6d4388345a0dc5ffa1de0bafa'
            '20482026371099db38e741eabd7d7ac6c11bd6467ee3add4e395aa56eccd4d42')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i ../libgme-3afa3ea124c5360ad561d7acb7ba97ae7497841d.patch
}

build() {
  mkdir "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DGME_BUILD_SHARED=ON \
    -DGME_BUILD_STATIC=ON \
    -DGME_BUILD_TESTING=OFF \
    -DGME_BUILD_EXAMPLES=OFF \
    -DGME_ENABLE_UBSAN=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/license.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/license.txt"
}
